package demo3;

class Animal {
    public String name;
    public int age;

    public void eat() {
        System.out.println("吃饭");
    }
}
class Cat extends Animal {
    public String color;

    public void eat() {//拥有同名的eat方法
        System.out.println(name+"吃猫粮");
    }
}
class Dog extends Animal {
    public void eat() {
        System.out.println(name+"吃狗粮");
    }
}
public class Test {
    public static void func(Animal animal) {
        animal.eat();
    }

    //这里可以返回子类对象 或者类自己本身的对象
    public static Animal func2() {
        //return new Animal();
        Cat cat = new Cat();
        Dog dog = new Dog();
        Animal animal = new Animal();
        return cat;
    }
    public static Cat func3() {
        //return new Animal();
        Cat cat = new Cat();
        return cat;
    }

    public static void main(String[] args) {
        Animal animal = func3();
    }

    public static void main2(String[] args) {
        Cat cat = new Cat();
        Dog dog = new Dog();
        func(cat);
        func(dog);
    }

    public static void main1(String[] args) {
        Animal animal = new Cat();//向上转型
        animal.eat();//调用子类的eat 动态绑定
        //animal.color 只能通过父类引用访问父类自己的成员
    }
}
